//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.qzsoft.jeemis.platform.security.oauth2;

import com.qzsoft.jeemis.common.utils.MessageUtils;
import com.qzsoft.jeemis.platform.security.service.ShiroService;
import com.qzsoft.jeemis.platform.security.user.UserDetail;
import com.qzsoft.jeemis.repository.sys.entity.BasicUserTokenEntity;
import java.util.Date;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Oauth2Realm extends AuthorizingRealm {

    private static final Long RENEWAL_EXPIRE = 900000L;

    private static final Long EXTENDED_EXPIRE = 300000L;

    @Autowired
    private ShiroService shiroService;

    public Oauth2Realm() {
    }

    public boolean supports(AuthenticationToken token) {
        return token instanceof Oauth2Token;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        UserDetail user = (UserDetail) principals.getPrimaryPrincipal();
        Set<String> permsSet = this.shiroService.getUserPermissions(user);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(permsSet);
        return info;
    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String accessToken = (String) token.getPrincipal();
        BasicUserTokenEntity tokenEntity = this.shiroService.getByToken(accessToken);
        Long currentTime = System.currentTimeMillis();
        if (tokenEntity != null && tokenEntity.getExpireDate().getTime() >= currentTime) {
            if (tokenEntity != null && tokenEntity.getExpireDate().getTime() - currentTime <= EXTENDED_EXPIRE) {
                Date now = new Date();
                Date expireTime = new Date(now.getTime() + RENEWAL_EXPIRE);
                tokenEntity.setExpireDate(expireTime);
            }

            UserDetail userDetail = this.shiroService.getUserDetail(tokenEntity.getUserId());
            userDetail.setToken(accessToken);
            if (userDetail.getStatus() == 0) {
                throw new LockedAccountException(MessageUtils.getMessage(10022));
            } else {
                SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userDetail, accessToken, this.getName());
                return info;
            }
        } else {
            throw new IncorrectCredentialsException(MessageUtils.getMessage(10021));
        }
    }

}
